package com.ds.home.proxy;

import com.alibaba.fastjson.annotation.JSONField;
import com.ds.home.EIDeviceEndPoint;
import com.ds.home.engine.AppEngine;
import com.ds.iot.*;
import com.ds.iot.enums.DeviceDataTypeKey;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DeviceEndPointProxy implements DeviceEndPoint {

    @JSONField(serialize=false)
    private EIDeviceEndPoint dbEndPoint;

    private String systemCode;

    @JSONField(serialize=false)
    public EIDeviceEndPoint getDbEndPoint() {
        return dbEndPoint;
    }

    public void setDbEndPoint(EIDeviceEndPoint dbEndPoint) {
        this.dbEndPoint = dbEndPoint;
    }

    public DeviceEndPointProxy(EIDeviceEndPoint dbEndPoint, String systemCode) {
        this.dbEndPoint = dbEndPoint;
        this.systemCode = systemCode;

    }

    @JSONField(serialize=false)
    public Device getDevice() {
        try {

            if (dbEndPoint.getDevice() != null) {
                return AppEngine.getEngine(systemCode).getDeviceById(dbEndPoint.getDevice().getDeviceid());
            }

        } catch (HomeException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setEndPointId(String endPointId) {
        dbEndPoint.setEndPointId(endPointId);

    }

    public String getEp() {
        return dbEndPoint.getEp();
    }

    public void setEp(String ep) {
        dbEndPoint.setEp(ep);
    }

    public String getHadeviceid() {
        return dbEndPoint.getHadeviceid();
    }

    public void setHadeviceid(String hadeviceid) {
        dbEndPoint.setHadeviceid(hadeviceid);
    }

    public String getIeeeaddress() {
        return dbEndPoint.getIeeeaddress();
    }

    public void setIeeeaddress(String ieeeaddress) {
        this.dbEndPoint.setIeeeaddress(ieeeaddress);
    }

    public String getName() {
        return dbEndPoint.getName();
    }

    public void setName(String name) {
        this.dbEndPoint.setName(name);
    }

    public String getNwkAddress() {
        return dbEndPoint.getNwkAddress();
    }

    public void setNwkAddress(String nwkAddress) {
        this.dbEndPoint.setNwkAddress(nwkAddress);
    }

    public String getProfileid() {
        return dbEndPoint.getProfileid();
    }

    public void setProfileid(String profileid) {
        this.dbEndPoint.setProfileid(profileid);
    }

    public void setCurrvalue(DeviceDataTypeKey name, String currvalue) {

        this.dbEndPoint.setCurrvalue(name.getType(), currvalue);
    }


    public Map<DeviceDataTypeKey, String> getCurrvalue() {

        Map<DeviceDataTypeKey, String> currMap = dbEndPoint.getCurrvalue();

        return currMap;
    }

    @JSONField(serialize=false)
    public List<ZNode> getAllZNodes() {

        List<ZNode> znodeList = new ArrayList<ZNode>();
        try {
            znodeList = AppEngine.getEngine(systemCode).getAllZNodeByEndPointId(this.getEndPointId());
        } catch (HomeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return znodeList;

    }


    public Sensortype getSensortype() {

        Sensortype sensortype = null;
        try {
            sensortype = AppEngine.getEngine(systemCode).getSensorTypesByNo(dbEndPoint.getSensortype().getType());
        } catch (HomeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return sensortype;

    }

    public void setSensortype(Sensortype sensorType) {
        if (sensorType!=null){
            dbEndPoint.setSensortype(sensorType.getType());
        }

    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DeviceEndPoint) {
            DeviceEndPoint ep = (DeviceEndPoint) obj;
            if (this.getEndPointId().equals(ep.getEndPointId())) {
                return true;
            }
            if (this.getIeeeaddress().equals(ep.getIeeeaddress())) {
                return true;
            }
        }

        return super.equals(obj);
    }

    @Override
    public String getEndPointId() {
        return dbEndPoint.getEndPointId();
    }

    @Override
    public Set<String> getAllZNodeIds() {
        return dbEndPoint.getAllZNodeIds();
    }


    @Override
    public String getDeviceId() {
        return dbEndPoint.getDeviceId();
    }

    @Override
    public void setCurrvalue(Map<DeviceDataTypeKey, String> currvalue) {

        Set<DeviceDataTypeKey> keySet=currvalue.keySet();

        for(DeviceDataTypeKey key:keySet){
            if (key!=null){
                updateCurrvalue(key,currvalue.get(key));
            }

        }
    }

    @Override
    public void updateCurrvalue(DeviceDataTypeKey name, String currvalue) {
        dbEndPoint.setCurrvalue(name.getType(), currvalue);
    }

}
